package com.pocketsweet.ui.uilib.adapter;

import java.util.ArrayList;
import java.util.List;

import com.pocketsweet.ui.uilib.AutoListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;

/**
 * Created by lzw on 14-9-25.
 */
public class BaseListAdapter<T> extends BaseAdapter implements OnScrollListener {
	protected Context ctx;
	protected LayoutInflater inflater;
	protected List<T> datas = new ArrayList<T>();
	protected ListItemClickHelp callback;

	private OnAdapterScrollListener onAdapterScrollListener;
	/**
	 * 当前listview是否属于滚动状态
	 */
	private boolean isScrolling;

	public boolean isScrolling() {
		return isScrolling;
	}

	public void setOnAdapterScrollListener(
			OnAdapterScrollListener onAdapterScrollListener) {
		this.onAdapterScrollListener = onAdapterScrollListener;
	}

	public BaseListAdapter(Context ctx) {
		initWithContext(ctx);
		// listView.setOnScrollListener(this);
	}

	public BaseListAdapter(Context ctx, AutoListView listview) {
		initWithContext(ctx);
		listview.setOnScrollListener(this);
	}

	public void initWithContext(Context ctx) {
		this.ctx = ctx;
		inflater = LayoutInflater.from(ctx);
	}

	public void initWithContext(Context ctx, ListItemClickHelp callback) {
		this.ctx = ctx;
		inflater = LayoutInflater.from(ctx);
		this.callback = callback;
	}

	public BaseListAdapter(Context ctx, List<T> datas) {
		initWithContext(ctx);
		this.datas = datas;
	}

	public BaseListAdapter(Context ctx, List<T> datas,
			ListItemClickHelp callback) {
		initWithContext(ctx);
		this.datas = datas;
		this.callback = callback;
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
	}

	public List<T> getDatas() {
		return datas;
	}

	public void addAll(List<T> subDatas) {
		datas.addAll(subDatas);
		notifyDataSetChanged();
	}

	public void addAll(int positon, List<T> subDatas) {
		datas.addAll(positon, subDatas);
		notifyDataSetChanged();
	}

	public void add(T object) {
		datas.add(object);
		notifyDataSetChanged();
	}

	public void remove(int position) {
		datas.remove(position);
		notifyDataSetChanged();
	}

	public void changItem(int position, T object) {
		datas.set(position, object);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (datas == null) {
			return 0;
		}
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return null;
	}

	public void clear() {
		datas.clear();
		notifyDataSetChanged();
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (null != onAdapterScrollListener) {
			onAdapterScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (null != onAdapterScrollListener) {
			onAdapterScrollListener.onScrollStateChanged(view, scrollState);
		}

		// 设置是否滚动的状态
		if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 不滚动状态
			isScrolling = false;
			this.notifyDataSetChanged();
		} else {
			isScrolling = true;
		}

	}
}
